﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MedVis
{
    class Data_3D
    {
        private ImageStack imagestacke = ImageStack.GetInstance;
        private ulong width, heigth, stack, volumen = 0;
        private double realWidth, realHeigth, realSpan, time = 0;
        private short datatype = 2;
        private char[] name = new char[128];
        private double[,] transformation = new double[4,4];
        private byte[] just = {0,0,0,0,0,0};

        private List<ushort> data = new List<ushort>();

        private List<Bitmap> images = new List<Bitmap>();

        private ushort red, green, blue, newPixelColor = 0;

        private int startHeigth, startWidth, endHeigth, endWidth = 0;

        private string projectName = "project.vmp";

        private string path = "";

        public void setPath(string path)
        {
            this.path = path;
        }

        public string ProjectName
        {
            set
            {
                projectName = value + ".vm2";
            }

            get
            {
                return projectName;
            }
        }

        public Data_3D(ulong x, ulong y, ulong z, double rx,
            double ry, double rz)
        {
            for(int i = 0; i <= (imagestacke.imageObjects.Count - 1); i++)
            {
                Bitmap bitmap = new Bitmap(imagestacke.imageObjects[i].path);
                AddBitmap(bitmap);
            }

            name[0] = 'V'; name[1] = 'M'; name[2] = '_'; name[3] = 'V';
            name[4] = 'O'; name[5] = 'L'; name[6] = 'U'; name[7] = 'M';
            name[8] = 'E'; name[9] = '_'; name[10] = 'P'; name[11] = 'H';
            name[12] = 'O'; name[13] = 'T'; name[14] = 'O'; name[15] = '_';
            name[16] = 'R'; name[17] = 'G'; name[18] = 'B'; name[19] = '_';
            name[20] = '5'; name[21] = '5'; name[22] = '5';
           

            for (int i = 23; i < name.Length; i++)
            {
                name[i] = ' ';
            }

            width = x; heigth = y; stack = z; volumen = 0;

            realWidth = rx; realHeigth = y; realSpan = rz; time = 0;

            transformation[0, 0] = 1; transformation[0, 1] = 0; transformation[0, 2] = 0; transformation[0, 3] = 0;
            transformation[1, 0] = 0; transformation[0, 1] = 1; transformation[1, 2] = 0; transformation[1, 3] = 0;
            transformation[2, 0] = 0; transformation[2, 1] = 0; transformation[2, 2] = 1; transformation[2, 3] = 0;
            transformation[3, 0] = 0; transformation[3, 1] = 0; transformation[3, 2] = 0; transformation[0, 3] = 1;
        }

        public void CreateVoxel()
        {
            for(int i = 0; i < images.Count; i++)
            {
                for (int j = startHeigth; j < (images[i].Height - endHeigth); j++)
                {
                    for (int k = startWidth; k < (images[i].Width - endWidth); k++)
                    {
                        Color PixelColor = images[i].GetPixel(k, j);

                        red = (ushort)(PixelColor.R / 8);
                        green = (ushort)(PixelColor.G / 8);
                        blue = (ushort)(PixelColor.B / 8);
                        newPixelColor = (ushort)(red + (green * (2 ^ 5)) + (blue * (2 ^ 10)));

                        data.Add(newPixelColor);
                    }
                }
            }
            SaveData();
        }

        public void AddBitmap(Bitmap bitmap)
        {
            images.Add(bitmap);
        }

        public void SetProbertys(int startW, int endW, int startH, int endH)
        {
            startWidth = startW; endWidth = endW;
            startHeigth = startH; endHeigth = endH;
        }

        private void SaveData()
        {
           
            FileStream fs = new FileStream(path, FileMode.Create);
          

            fs.Write(BitConverter.GetBytes(width), 0, BitConverter.GetBytes(width).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(heigth), 0, BitConverter.GetBytes(heigth).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(stack), 0, BitConverter.GetBytes(stack).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(volumen), 0, BitConverter.GetBytes(volumen).Length);
            fs.Flush();

            fs.Write(BitConverter.GetBytes(realWidth), 0, BitConverter.GetBytes(realWidth).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(realHeigth), 0, BitConverter.GetBytes(realHeigth).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(realSpan), 0, BitConverter.GetBytes(realSpan).Length);
            fs.Flush();
            fs.Write(BitConverter.GetBytes(time), 0, BitConverter.GetBytes(time).Length);
            fs.Flush();

            fs.Write(BitConverter.GetBytes(datatype), 0, BitConverter.GetBytes(datatype).Length);
            fs.Flush();

            for (int i = 0; i < name.Length; i++)
            {
                fs.Write(BitConverter.GetBytes(name[i]), 0, BitConverter.GetBytes(name[i]).Length);
                fs.Flush();
            }


            for (int z = 0; z < data.Count; z++)
            {
                fs.Write(BitConverter.GetBytes(data[z]), 0, BitConverter.GetBytes(data[z]).Length);
                fs.Flush();
            }

            fs.Close();

        }
    }
}
